package com.qingmi.farmer.data.repository.data;

import com.qingmi.farmer.data.DataSetting;
import com.wjjun.base.data.cache.CacheProviders;
import com.wjjun.base.data.db.DBManager;
import com.wjjun.base.data.db.IDBManager;
import com.qingmi.farmer.data.model.GoodsInfo;
import com.qingmi.farmer.data.model.GoodsOrder;
import com.qingmi.farmer.data.model.GoodsType;
import com.qingmi.farmer.data.model.Supplier;
import com.qingmi.farmer.data.model.Trolley;
import com.qingmi.farmer.data.repository.api.GoodsApiRepository;
import com.qingmi.farmer.data.request.GetGoodsListReq;
import com.qingmi.farmer.data.request.GetGoodsOrderListReq;
import com.qingmi.farmer.data.request.GetGoodsTypeReq;
import com.qingmi.farmer.data.request.GetSupplierReq;
import com.wjjun.base.data.respository.BaseDataRepository;
import com.wjjun.basemvpvm.data.executor.PostExecutionThread;
import com.wjjun.basemvpvm.data.executor.ThreadExecutor;
import com.wjjun.basemvpvm.data.model.PageEntity;
import com.wjjun.basemvpvm.data.observer.DefaultObserver;
import com.wjjun.basemvpvm.data.request.SimpleRequest;

import java.util.List;

import javax.inject.Inject;

import io.rx_cache2.DynamicKey;
import io.rx_cache2.EvictProvider;

/**
 * 生资模块
 * Created by 67535 on 2018/5/23.
 */
public class GoodsDataRepository extends BaseDataRepository<GoodsApiRepository> {

    private IDBManager dbManager;

    @Inject
    public GoodsDataRepository(GoodsApiRepository apiRepository, ThreadExecutor threadExecutor,
                               PostExecutionThread postExecutionThread,
                               DBManager dbManager, CacheProviders cacheProviders) {

        super(apiRepository, threadExecutor, postExecutionThread, cacheProviders);
        this.dbManager = dbManager;
    }

    /**
     * 获取商品列表
     */
    public void getGoodsList(DefaultObserver<PageEntity<List<GoodsInfo>>> observer, GetGoodsListReq param) {
        execute(cacheProviders.getGoodsList(apiRepository.getGoodsList(param), new EvictProvider(true)), observer);
    }

    /**
     * 获取单个商品详情
     */
    public void getGoodsDetail(DefaultObserver<GoodsInfo> observer, SimpleRequest param) {
        execute(apiRepository.getGoodsDetail(param), observer);
    }


    /**
     * 获取厂商信息
     */
    public void getSupplier(DefaultObserver<List<Supplier>> observer, GetSupplierReq param) {
        execute(cacheProviders.getSupplier(apiRepository.getSupplier(param), new DynamicKey(param.getClassId()),  new EvictProvider(false)), observer);
    }

    /**
     * 获取商品类型
     */
    public void getGoodsType(DefaultObserver<List<GoodsType>> observer, GetGoodsTypeReq param) {
        execute(cacheProviders.getGoodsType(apiRepository.getGoodsType(param), new DynamicKey(param.getClassId()), new EvictProvider(false)), observer);
    }

    /**
     * 获取购物车数据
     */
    public void getTrolley(DefaultObserver<List<Trolley>> observer) {
        execute(apiRepository.getTrolley(), observer);
    }


    /**
     * 获取订单列表
     */
    public void getGoodsOrderList(DefaultObserver<List<GoodsOrder>> observer, GetGoodsOrderListReq param) {
        execute(apiRepository.getGoodsOrderList(param), observer);
    }

    /**
     * 获取待审核的订单列表
     */
    public void getPendingCheckGoodsOrderList(DefaultObserver<List<GoodsOrder>> observer) {
        getGoodsOrderList(observer, new GetGoodsOrderListReq(DataSetting.GOODS_ORDER_STATUS_PENDING_CHECK));
    }

    /**
     * 获取待确认的订单列表
     */
    public void getPendingConfirmGoodsOrderList(DefaultObserver<List<GoodsOrder>> observer) {
        getGoodsOrderList(observer, new GetGoodsOrderListReq(DataSetting.GOODS_ORDER_STATUS_PENDING_CONFIRM));
    }


    /**
     * 获取已确认的订单列表
     */
    public void getConfirmedGoodsOrderList(DefaultObserver<List<GoodsOrder>> observer) {
        getGoodsOrderList(observer, new GetGoodsOrderListReq(DataSetting.GOODS_ORDER_STATUS_CONFIRMED));
    }

}
